package tech.aistar.service.impl.authordis;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.Notes;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.mapper.authordis.INoteTravelerDisMapper;
import tech.aistar.service.authordis.INoteTravelerDisService;
import tech.aistar.vo.travelerlist.NoteAndPersonVo;

import java.util.*;

/**
 * Created by lenovo on 2019/10/2.
 */
@SuppressWarnings("all")
@Service
@Transactional
public class NoteTravelerDisServiceImpl implements INoteTravelerDisService {

    @Autowired
    private INoteTravelerDisMapper noteTravelerDisMapper;


    /**
     * 查询达人游记 - 热门推荐
     * 在进行排序后,进行分页的展示
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Map<String,Object> findNoteByHot(Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum, pageSize);

        //游记的获取
        List<Notes> notes = noteTravelerDisMapper.findNoteByHot();
        //分页数据 - 第三方实体类存入
        PageInfo<Notes> pageInfo = new PageInfo<>(notes);
//        获取分页后的游记集合
        List<Notes> notesList = pageInfo.getList();

        //存储游记和作者信息
        List<NoteAndPersonVo> noteAndPersonVos = new ArrayList<>();

//        遍历添加用户信息
        Iterator<Notes> iter = notesList.iterator();
        while (iter.hasNext()) {
            Notes n = iter.next();
//          获取游记作者id
            Integer autuorId = n.getAutuorId();
//          获取游记作者id
            Integer userId = noteTravelerDisMapper.findUserIdByAuthorId(autuorId);
//          获取用户信息
            UserTable u = noteTravelerDisMapper.findUserById(userId);
//          将用户信息和游记信息放入到第三方实体类中
            NoteAndPersonVo noteAndPersonVo = new NoteAndPersonVo();
            noteAndPersonVo.setNotes(n);
            noteAndPersonVo.setUserTable(u);
            //将第三方实体类放入到集合中
            noteAndPersonVos.add(noteAndPersonVo);

        }
//        使用map集合存储分页集合和第三方实体类
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        map.put("noteAndPersonVos",noteAndPersonVos);
        return map;

    }

    /**
     *查询达人游记 - 一定时间范围内
     * @param time
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Map<String,Object> findNoteByTime(String time, Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum, pageSize);

        //游记的获取
        List<Notes> notes = noteTravelerDisMapper.findNoteByTime(time);
        //分页数据 - 第三方实体类存入
        PageInfo<Notes> pageInfo = new PageInfo<>(notes);
//        获取分页后的游记集合
        List<Notes> notesList = pageInfo.getList();

        //存储游记和作者信息
        List<NoteAndPersonVo> noteAndPersonVos = new ArrayList<>();

//        遍历添加用户信息
        Iterator<Notes> iter = notesList.iterator();
        while (iter.hasNext()) {
            Notes n = iter.next();
//          获取游记作者id
            Integer autuorId = n.getAutuorId();
//          获取游记作者id
            Integer userId = noteTravelerDisMapper.findUserIdByAuthorId(autuorId);
//          获取用户信息
            UserTable u = noteTravelerDisMapper.findUserById(userId);
//          将用户信息和游记信息放入到第三方实体类中
            NoteAndPersonVo noteAndPersonVo = new NoteAndPersonVo();
            noteAndPersonVo.setNotes(n);
            noteAndPersonVo.setUserTable(u);
            //将第三方实体类放入到集合中
            noteAndPersonVos.add(noteAndPersonVo);

        }
//        使用map集合存储分页集合和第三方实体类
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        map.put("noteAndPersonVos",noteAndPersonVos);
        return map;
    }

    /**
     * 根据游记作者id获取用户id
     * @param authorId
     * @return
     */
    @Override
    public Integer findUserIdByAuthorId(Integer authorId) {
        Integer userId = noteTravelerDisMapper.findUserIdByAuthorId(authorId);
        return userId;
    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @Override
    public UserTable findUserById(Integer id) {
        UserTable userTable = noteTravelerDisMapper.findUserById(id);
        return userTable;
    }

}
